package com.boss.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import com.boss.example.entity.vo.CommonResult;
import com.boss.example.entity.vo.UserVO;
import com.boss.example.entity.vo.UUUsersVO;
import com.boss.example.entity.dto.UserDTO;
import com.boss.example.service.UserService;
import com.boss.example.exception.UsersException;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public CommonResult userLogin(@RequestBody UserVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        UserDTO getUsersDTO =userService.userLogin(usersDTO.getUsername(),usersDTO.getUserPwd());
        if(getUsersDTO != null){
            BeanUtils.copyProperties(getUsersDTO,usersVO);
            return new CommonResult(200,"登陆成功",usersVO);
        }
        return new CommonResult(400,"登陆失败",null);
    }

    @PostMapping("/delete")
    public CommonResult userDelete(@RequestBody UserVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        int result = userService.userDelete(usersDTO.getUsername());
        if(result>0){
            return new CommonResult(200,"删除成功",result);
        }else {
            return new CommonResult(400,"删除失败",null);
        }
    }

    @PostMapping("/insert")
    public CommonResult userInsert(@RequestBody UserVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        int result = userService.userInsert(usersDTO);
        if(result>0){
            return new CommonResult(200,"插入成功",result);
        }else {
            return new CommonResult(400,"插入失败",null);
        }
    }

    @PostMapping("/updatePwd")
    public CommonResult userUpdatePwd(@RequestBody UserVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        int result = userService.userUpdatePwd(usersDTO);
        if(result>0){
            return new CommonResult(200,"成功更改密码",result);
        }else {
            return new CommonResult(400,"没能更改密码",null);
        }
    }

    @PostMapping("/updateRole")
    public CommonResult userUpdateRole(@RequestBody UserVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        UserDTO adminDTO =  userService.getUserIsAdmin(usersDTO.getUserId());
        if( adminDTO != null ){
            log.info("********是管理员");
            userService.userUpdatePwd(usersDTO);
        }else{
            return new CommonResult(400,"权限不足");
        }
        return new CommonResult(200,"修改权限成功");
    }

    @GetMapping("/getAll")
    public CommonResult getAllUser(){
        List<UserDTO> userDTOList = userService.getAllUser();
        if(userDTOList != null ){
            List<UserVO> userVOList = new ArrayList<>();
            BeanUtils.copyProperties(userDTOList,userVOList);
            log.info(userVOList.toString());
            return new CommonResult(200,"查询成功",userVOList);
        }else{
            return new CommonResult(400,"查询不足");
        }
    }

    @GetMapping("/get/{id}")
    public CommonResult getUserById(@PathVariable Integer id){
        log.info(String.valueOf(id));
        UserDTO userDTO = userService.getUserById(id);
        if(userDTO != null ){
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userDTO,userVO);
            return new CommonResult(200,"查询成功",userVO);
        }else{
            return new CommonResult(400,"查询失败");
        }
    }

}
